package updater;

import java.io.File;
import java.io.IOException;

import javax.swing.JApplet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;

public class UpdaterMain extends Shell {

	private static File settingsFile = new File("./settings");
	
	MapShareVersionInfo info = new MapShareVersionInfo();	

	private Label lblCurrentVersion;
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			UpdaterMain shell = new UpdaterMain(display);
			shell.parseArgs(args);
			shell.info.loadFile(settingsFile);
			shell.lblCurrentVersion.setText(shell.info.version.toString());
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void parseArgs(String args[]) throws IOException{
		if(args.length > 0){
			settingsFile = new File(args[0]);
		}
	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public UpdaterMain(Display display) {
		super(display, SWT.SHELL_TRIM);
		addShellListener(new ShellAdapter() {
			@Override
			public void shellActivated(ShellEvent e) {
				
			}
		});
		setLayout(new GridLayout(2, false));
		
		Label lblYourVersion = new Label(this, SWT.NONE);
		lblYourVersion.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblYourVersion.setText("Your Version:");
		
		lblCurrentVersion = new Label(this, SWT.NONE);
		lblCurrentVersion.setText("");
		
		Label lblServerVersion = new Label(this, SWT.NONE);
		lblServerVersion.setText("Server Version:");
		lblServerVersion.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		lblNewLabel.setText("Checking Version...");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("MapShare Updater");
		pack();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
